package com.hzdl.apple.controller.backCotroll;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.hzdl.apple.bean.cart.Order;
import com.hzdl.apple.service.back.BackOrderService;
import com.hzdl.apple.utils.HttpUtils;

import net.sf.json.JSONObject;
@WebServlet("/BackOrder/*")
public class OrderServlet extends HttpServlet{
	private BackOrderService service;
	public void init() throws ServletException {
		super.init();
		service = new BackOrderService();
		getServletContext().setAttribute("path", getServletContext().getContextPath());
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		String baseUri = req.getRequestURI();
		JSONObject json = new JSONObject();
		if (baseUri.contains("/toOrderEdit")) {
			List<Order> list = service.getAllOrder();
			req.setAttribute("data", JSON.toJSONString(list));
			HttpUtils.forward("back/back-OrderEdit.jsp", req, resp);
		}else if(baseUri.contains("/DeleteOrder")){
			String oID = req.getParameter("oID");
			boolean b = service.deleteOrder(oID);
			if(b){
				json.put("success", "true");
				resp.getWriter().write(json.toString());
			}else{
				json.put("error", "false");
				resp.getWriter().write(json.toString());
			}
		}else if(baseUri.contains("/getCanOrder")){
			List<Order> list = service.getCanOrder();
			req.setAttribute("data", JSON.toJSONString(list));
			HttpUtils.forward("back/back-CanOrderEdit.jsp", req, resp);
		}else if(baseUri.contains("/updateCanOrder")){
			String oID = req.getParameter("oID");
			boolean b = service.updateCanOrder(oID);
			if(b){
				json.put("success", "true");
				resp.getWriter().write(json.toString());
			}else{
				json.put("error", "false");
				resp.getWriter().write(json.toString());
			}
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
	
}
